//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System.Configuration;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Management.Instrumentation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;

namespace EntLibContrib.Resource.Configuration.Manageability
{
	/// <summary>
	/// Represents the general configuration information for the Resource Application Block.
	/// </summary>
	/// <seealso cref="EntLibContrib.Resource.Configuration.ResourceSettings"/>
	/// <seealso cref="ConfigurationSectionSetting"/>
	[ManagementEntity]
	public class ResourceBlockSetting : ConfigurationSectionSetting
	{
		#region Fields
		private string defaultResourceManager;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the name of the default resource manager for the represented configuration section.
		/// </summary>
		/// <value>The default resource manager.</value>
		/// <seealso cref="EntLibContrib.Resource.Configuration.ResourceSettings.DefaultResourceManager" />
		[ManagementConfiguration]
		public string DefaultResourceManager
		{
			get { return defaultResourceManager; }
			set { defaultResourceManager = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ResourceBlockSetting"/> class.
		/// </summary>
		/// <param name="settings">The resource manager configuration.</param>
		/// <param name="defaultResourceManager">The default resource manager.</param>
		public ResourceBlockSetting(ResourceSettings settings, string defaultResourceManager) : base(settings)
		{
			this.defaultResourceManager = defaultResourceManager;
		}
		#endregion

		#region Public Static Methods
		/// <summary>
		/// Returns an enumeration of the published <see cref="ResourceBlockSetting"/> instances.
		/// </summary>
		/// <returns></returns>
		[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
		[ManagementEnumerator]
		public static IEnumerable<ResourceBlockSetting> GetInstances()
		{
			return GetInstances<ResourceBlockSetting>();
		}

		/// <summary>
		/// Returns the <see cref="ResourceBlockSetting"/> instance corresponding to the provided values for the key properties.
		/// </summary>
		/// <param name="applicationName">Name of the application.</param>
		/// <param name="sectionName">Name of the section.</param>
		/// <returns>
		/// The published <see cref="ResourceBlockSetting"/> instance specified by the values for the key properties,
		/// or <see langword="null"/> if no such an instance is currently published.
		/// </returns>
		[ManagementBind]
		public static ResourceBlockSetting BindInstance(string applicationName, string sectionName)
		{
			return BindInstance<ResourceBlockSetting>(applicationName, sectionName);
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Saves the changes on the <see cref="ResourceBlockSetting"/> to its corresponding configuration object.
		/// </summary>
		/// <param name="sourceElement">The configuration object on which the changes must be saved.</param>
		/// <returns>
		/// 	<see langword="true"/> if changes have been saved, <see langword="false"/> otherwise.
		/// </returns>
		/// <remarks>Must be overridden by subclasses to perform the actual save.</remarks>
		protected override bool SaveChanges(ConfigurationElement sourceElement)
		{
			return ResourceSettingsWmiMapper.SaveChanges(this, sourceElement);
		}
		#endregion
	}
}
